from ronglian_sms_sdk import SmsSDK
from flask import *
import json, redis

# 发送短信验证码
class SmsCode():
    def __init__(self):
        accId = current_app.config.get('ACCID')
        accToken = current_app.config.get('ACCTOKEN')
        appId = current_app.config.get('APPID')
        self.sdk = SmsSDK(accId, accToken, appId)
        self.tid = current_app.config.get('TID')

    def sms_code(self, mobile, code):
        # 发送验证码
        ser = self.sdk.sendMessage(self.tid, mobile, (code, '5'))

        # 存储到redis数据库
        pool = redis.ConnectionPool()
        r = redis.Redis(connection_pool=pool)
        r.set(mobile, code, ex=3600)

        ser_str = json.loads(ser)
        if ser_str['statusCode'] == '000000':
            return True
        return False


if __name__ == '__main__':
    from app import app
    with app.app_context():
        SmsCode().sms_code('17559511999', '666')